package com.guaoran.algorithm.binary.search;

/**
 * @Auth gucheng
 * @Desc 二分查找法
 * @Date 2020/12/28 下午3:27
 */
public class BasicBinarySearch {
    public static void main(String[] args) {
        int [] num = {1,4,9,10,12,16,25,58,77,88,99};
        System.out.println(get(num,99));
    }
    public static int get(int[] num,int target){
        if(num  == null || num.length == 0){
            return  -1;
        }
        int start = 0;
        int end = num.length - 1;
        int mid;

        while(start + 1 < end){
            mid = start + (end - start) / 2;
            if(num[mid] == target) {
                return mid;
            }
            if(num[mid] > target) {
                end = mid;
            } else {
                start = mid;
            }
        }
        if(num[start]==target) {
            return start;
        }
        if(num[end] == target) {
            return end;
        }
        return -1;
    }
}
